/**
 * 
 */
package org.andengine.shmup;

import java.util.HashMap;

import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.pool.GenericPool;

/**
 * @author Calvin
 *
 */
public class EnemyPool extends GenericPool<Enemy>{
	
	private TiledTextureRegion mTextureRegion;
	private float cam_w = 480;
    private float cam_h = 720;
	private static HashMap<String, MotionType> type_map = new HashMap<String, MotionType>();
	
	public EnemyPool(TiledTextureRegion pTextureRegion, int cw, int ch){
		if (pTextureRegion == null) {
		// Need to be able to create a Sprite so the Pool needs to have a TextureRegion
			throw new IllegalArgumentException("The texture region must not be NULL");
		}
		mTextureRegion = pTextureRegion;
		cam_w = cw;
		cam_h = ch;

		type_map.put("ARC_POS", MotionType.ARC_POS);
		type_map.put("ARC_NEG", MotionType.ARC_NEG);
		type_map.put("GAUSSIAN", MotionType.GAUSSIAN);
		type_map.put("LINEAR", MotionType.LINEAR);
		type_map.put("LINEAR_V", MotionType.LINEAR_V);
		type_map.put("LINEAR_SPREAD", MotionType.LINEAR_SPREAD);
		type_map.put("LOOP", MotionType.LOOP);
		type_map.put("PARABOLA", MotionType.PARABOLA);
	}

	 @Override
	  protected void onHandleRecycleItem(final Enemy nme) {
		nme.reset();
	   nme.setIgnoreUpdate(true);
	   nme.setVisible(false);
	  }

	
	@Override
	protected Enemy onAllocatePoolItem() {
		// TODO Auto-generated method stub
		return new Enemy(cam_w/2, -1*(cam_h/10.0f), mTextureRegion);
	}
	
	@Override
	protected void onHandleObtainItem(final Enemy en) {
	   en.reset();
	}

	public Enemy obtainPoolItem(String trajectory, float nX, float nY, int pRotation){
		Enemy e = this.obtainPoolItem();
		e.setIgnoreUpdate(false);
		e.setVisible(true);
		
		e.setT(type_map.get(trajectory));
		if(e.getInitX() != nX){ e.setInitX(nX);	}
		if(e.getInitY() != nY){	e.setInitY(nY);	}
		e.setPosition(e.getInitX(), e.getInitY());
		e.setInitRotation(pRotation);
		return e;
	}
	public float getCameraWidth(){
		return cam_w;
	}
	
	public float getCameraHeight(){
		return cam_h;
	}

	public int getEnemyHeight() {
		return mTextureRegion.getTileHeight();
	}

	public int getEnemyWidth() {
		return mTextureRegion.getTileWidth();
	}

}
